package com.atguigu.study.base.locks;

import java.util.concurrent.TimeUnit;

/**
 * @Author 快点干早点散
 * @Create 2025/4/24 18:38
 * Description:
 */
public class DeadLockDome2 {
    static String lock1 = new String();
    static String lock2 = new String();

    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (lock1) {
                System.out.println(Thread.currentThread().getName() + "使用A锁,还想要B锁");

//                try {
//                    TimeUnit.SECONDS.sleep(0);
                    synchronized (lock2) {
                        System.out.println("获得B锁");
//                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (lock2) {
                System.out.println(Thread.currentThread().getName() + "使用B锁,还想要A锁");

//                try {
//                    TimeUnit.SECONDS.sleep(1);
                    synchronized (lock1) {
                        System.out.println("获得A锁");
                    }
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
                }

        }, "B").start();
    }
}
